Page({
    data: {
        begindate: '2000-01-01',
        today: '2000-01-01',
        enddate: '2021-11-09',
        tripBeginTime: '',
        tripEndTime: '',
        tripDuring: 1,
        isPub: false,
        title: '',
    },

    getDaysBetween: function (dateString1, dateString2) {
        var startDate = Date.parse(dateString1);
        var endDate = Date.parse(dateString2);
        console.log(dateString1, dateString2)
        if (startDate > endDate) {
            console.log(0)
            return 0;
        } else if (startDate == endDate) {
            console.log(1)
            return 1;
        } else {
            var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000) + 1;
            console.log(days)
            return days;
        }
    },

    getToday: function () {
        var now = new Date()
        var year = now.getFullYear(); //得到年份
        var month = now.getMonth(); //得到月份
        var date = now.getDate(); //得到日期
        month = month + 1;
        if (month < 10) month = "0" + month;
        if (date < 10) date = "0" + date;
        var todaytime = "";
        todaytime = year + "-" + month + "-" + date
        return {
            todaytime,
            beginTime: year - 1 + "-" + month + "-" + date,
            endTime: year + 1 + "-" + month + "-" + date
        }
    },

    bindBeginDateChange: async function (e) {
        console.log('BeginDateChange', e.detail.value)
        this.setData({
            tripBeginTime: e.detail.value
        })
        if (Date.parse(e.detail.value) > Date.parse(this.data.tripEndTime)) {
            this.setData({
                tripEndTime: e.detail.value,
                tripDuring: 1
            })
        } else {
            this.setData({
                tripDuring: this.getDaysBetween(e.detail.value, this.data.tripEndTime)
            })
        }

    },

    bindEndDateChange: function (e) {
        console.log('EndDateChange', e.detail.value)
        this.setData({
            tripEndTime: e.detail.value
        })
        if (Date.parse(e.detail.value) < Date.parse(this.data.tripBeginTime)) {
            this.setData({
                tripBeginTime: e.detail.value,
                tripDuring: 1
            })
        } else {
            this.setData({
                tripDuring: this.getDaysBetween(this.data.tripBeginTime, e.detail.value)
            })
        }
    },

    getPub: function (e) {
        console.log(e.detail.value)
        this.setData({
            isPub: e.detail.value
        })
    },

    getTitle: function (e) {
        this.setData({
            title: e.detail.value
        })
    },


    submitTrip: async function () {
        if (this.data.title == '') {
            wx.showToast({
                title: '请输入行程标题',
                icon: 'error'
            })
        } else {
            wx.showLoading({
                title: '加载中',
            })
            var image = ''
            var that = this
            if (this.data.mainImage) {
                await wx.cloud.uploadFile({
                    cloudPath: 'trip/' + new Date().getTime() + '.jpg', // 上传至云端的路径
                    filePath: that.data.mainImage, // 小程序临时文件路径
                    success: res => {
                        console.log(res.fileID) //输出上传后图片的返回地址
                        wx.cloud.callFunction({
                            name: 'addTrip',
                            data: {
                                title: this.data.title,
                                tripBeginTime: Date.parse(this.data.tripBeginTime),
                                tripEndTime: Date.parse(this.data.tripEndTime),
                                isPub: this.data.isPub,
                                tripDuring: this.data.tripDuring,
                                image: res.fileID
                            }
                        }).then(res => {
                            console.log('success')
                            wx.showToast({
                                title: '添加成功',
                                duration: 2000,
                                success: function () {
                                    setTimeout(function () {
                                        console.log(res.result._id)
                                        wx.redirectTo({
                                          url: '/pages/tripDetail/tripDetail?id=' + res.result._id,
                                        })
                                    }, 2000)
                                }
                            })
                        })
                    },
                    fail: err => {
                        console.log(err)
                        wx.showToast({
                            title: "封面上传失败",
                            icon: 'error'
                        })
                    }
                })
            } else {
                wx.cloud.callFunction({
                    name: 'addTrip',
                    data: {
                        title: this.data.title,
                        tripBeginTime: Date.parse(this.data.tripBeginTime),
                        tripEndTime: Date.parse(this.data.tripEndTime),
                        isPub: this.data.isPub,
                        tripDuring: this.data.tripDuring,
                        image: 'cloud://aaa111-9gh9fp5ieac4de5d.6161-aaa111-9gh9fp5ieac4de5d-1304582054/trip/123.jpeg'
                    }
                }).then(res => {
                    console.log('success')
                    wx.showToast({
                        title: '添加成功',
                        duration: 2000,
                        success: function () {
                            setTimeout(function () {
                                console.log(res.result._id)
                                wx.redirectTo({
                                    url: '/pages/tripDetail/tripDetail?id=' + res.result._id,
                                  })
                            }, 2000)
                        }
                    })
                })
            }
        }
    },

    chooseImages: function () {
        console.log("选择图片")
        var that = this
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFilePaths
                console.log(tempFilePaths)
                that.setData({
                    mainImage: tempFilePaths[0]
                })
            }
        })
    },

    onLoad: function (params) {
        console.log(this.getToday())
        this.setData({
            begindate: this.getToday().beginTime,
            today: this.getToday().todaytime,
            enddate: this.getToday().endTime,
            tripBeginTime: this.getToday().todaytime,
            tripEndTime: this.getToday().todaytime,
            tripDuring: 1
        })
    }
})